package com.tiezhu.springdemo.controller;

import com.tiezhu.springdemo.bean.Pet;
import com.tiezhu.springdemo.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PetController {
    @Autowired
    private PetMapper petMapper;


    @GetMapping("/queryPet")
    @ResponseBody
    public Map<String,Object> queryPerByMasterName(@RequestParam("master_name") String master_name){
        Map<String,Object> hashMap = new LinkedHashMap<>();
        List<Pet> petList = petMapper.queryPetByMasterName(master_name);
        System.out.println(petList);
        if (petList.size() == 0){
            hashMap.put("status",1);
            hashMap.put("msg","宠物不存在");
            return hashMap;
        }
        else {
            hashMap.put("status",0);
            hashMap.put("msg","宠物存在");
            for (int i = 0; i <petList.size() ; i++) {
                Pet pet = petList.get(i);
                hashMap.put("Pet"+(i+1),pet);
            }
            return hashMap;
        }
    }
    @PostMapping("/addPet")
    public Map<String,Object> addPet(@RequestBody Pet pet){
        Map<String,Object> hashMap = new LinkedHashMap<>();
        List<Pet> petList = petMapper.queryPetByPetNameAndMasterName(pet.getPet_name(),pet.getMaster_name());
        if (petList.size() != 0){
            hashMap.put("status",1);
            hashMap.put("msg","宠物名不可重复");
            return hashMap;
        }else{
            int i = petMapper.addPet(pet);
            if (i==1){
                hashMap.put("status",0);
                hashMap.put("msg","添加成功");
                hashMap.put("Pet",pet);
                return hashMap;
            }else {
                hashMap.put("status",1) ;
                hashMap.put("msg","添加失败");
                hashMap.put("Pet",pet);
                return hashMap;
            }
        }
    }
    @GetMapping("/updatePet")
    public Map<String,Object> updatePet(@RequestBody Pet pet){
        Map<String,Object> hashMap= new LinkedHashMap<>();
        Pet petAlternates = petMapper.queryPetByPetIdAndMasterName(pet);
        System.out.println(pet.toString());
        int i = petMapper.updatePet(new Pet((pet.getPet_id()),
                pet.getPet_name(),
                pet.getPet_age(),
                pet.getPet_weight(),
                pet.getMaster_name()));
        List<Pet> petList = petMapper.queryPetByPetNameAndMasterName(pet.getPet_name(),pet.getMaster_name());

        if (i == 1){
            pet = petList.get(0);
            pet.setPet_oldname(petAlternates.getPet_name());
            hashMap.put("status",0);
            hashMap.put("msg","修改成功");
            hashMap.put("Pet",pet);
            return hashMap;
        }else {
            pet = petList.get(0);
            hashMap.put("status",1);
            hashMap.put("msg","修改失败");
            hashMap.put("Pet",pet);
            return hashMap;
        }
    }
    @PostMapping("/deletePet")
    @ResponseBody
    public HashMap<String ,Object> deletePetByPetName(@RequestBody Pet pet){
        HashMap<String ,Object> hashMap = new LinkedHashMap<>();
        int i = petMapper.deletePetByPetName(pet.getPet_name(),pet.getMaster_name());
        if (i == 1){
            hashMap.put("status",0);
            hashMap.put("msg","删除成功");
            return hashMap;
        }else {
            hashMap.put("status",1);
            hashMap.put("msg","删除失败");
            return hashMap;
        }
    }
}
